package com.sgb.kjwl.viewmodel.home;

import android.content.Intent;

import androidx.databinding.ObservableField;

import com.google.gson.reflect.TypeToken;
import com.sgb.goods.entity.GoodsItemEntity;
import com.sgb.goods.view.activity.inquiry.NewPurchasesActivity;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson3;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.AreaChooseEntity;
import com.sgb.kjwl.model.entity.user.ConcreteEntity;
import com.sgb.kjwl.model.entity.user.GoodsListEntity;
import com.sgb.kjwl.model.entity.user.MaterChooseEntity;
import com.sgb.kjwl.model.entity.user.RecommendListEntity;
import com.sgb.kjwl.model.entity.user.TwoOrThreeLevelEntity;
import com.sgb.kjwl.repository.MaterialRepertory;
import com.sgb.kjwl.view.ui.fragment.home.GoodsMainGoodsListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.ACAdminDialogUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.MenuEntity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.swgk.core.util.ACAdminDialogUtils.MK_OURINQUIRYPRICELIST;

/**
 * @author Administrator
 */
public class GoodsMainGoodsListModel extends BaseViewModel {
    public ObservableField<MaterialFilterJson3> materialFilterJson = new ObservableField<>(new MaterialFilterJson3());
    public ObservableField<String> areaCode = new ObservableField<>("");
    private GoodsMainGoodsListFragment mFragment;
    private MaterialRepertory mRepertory;

    public GoodsMainGoodsListModel(GoodsMainGoodsListFragment fragment, MaterialRepertory repertory) {
        this.mFragment = fragment;
        this.mRepertory = repertory;
    }

    public void getFilterBrandModel(String id) {
//        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getMaterialAPI().getBrandChoose(mRepertory.getAreaCode(), id).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showBrandPopWindow(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    public void getMaterialBrand(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("areaCode", mRepertory.getAreaCode());
        map.put("id", id);
//        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getMaterChoose(map).enqueue(new Callback<BaseEntity<List<MaterChooseEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MaterChooseEntity>>> call, Response<BaseEntity<List<MaterChooseEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<MaterChooseEntity> list = response.body().getData();
                    for (MaterChooseEntity materChooseEntity : list) {
                        materChooseEntity.getType().add(0, new MaterChooseEntity.TypeBean("全部", ""));
                    }
                    mFragment.setMaterialBrandData(list);
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MaterChooseEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getFilterDistance(String cid1) {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getGoodsMaterialDistanceData(cid1).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.setDistanceData(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getAreaData(boolean isRegionalConnection) {
//        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Type type = new TypeToken<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>() {
        }.getType();
        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.AREA_JSON, type);
        if (obj != null) {
            List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> dataList = (List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>) obj;
            if (isRegionalConnection) {
                mFragment.setAddressData(dataList, mRepertory.getAreaCode());
            } else {
                mFragment.setAddressData(dataList, "");
            }
        } else {
            APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
//                DialogHelper.dismissProgressDialog();
                    if (response.body() == null) {
                        return;
                    }
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        if (isRegionalConnection) {
                            mFragment.setAddressData(response.body().getData(), mRepertory.getAreaCode());
                        } else {
                            mFragment.setAddressData(response.body().getData(), "");
                        }
                    } else {
                        MToast.showToast(mFragment.getContext(), response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
                }
            });
        }
    }

    public void goLogin() {
        int type = ACAdminDialogUtils.isPermissionForAsk(MK_OURINQUIRYPRICELIST, true);
        if (type == ACAdminDialogUtils.TYPE_NORMAL) {
            Intent createGood = new Intent(mFragment.getContext(), NewPurchasesActivity.class);
            createGood.putExtra("GOOD_EDIT", false);
            createGood.putExtra("GOOD_ADD", false);
            createGood.putExtra("GOOD_ENTITY", new GoodsItemEntity());
            mFragment.requireContext().startActivity(createGood);
        } else {
            ACAdminDialogUtils.showACDialogForGoods(type, mFragment.getContext());
        }
    }

    public void getTwoOrThreeLevelData(String id) {
//        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().findTwoOrThreeLevelData(mRepertory.getAreaCode(), id).enqueue(new Callback<BaseEntity<List<TwoOrThreeLevelEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TwoOrThreeLevelEntity>>> call, Response<BaseEntity<List<TwoOrThreeLevelEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.setTwoOrThreeLevelData(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TwoOrThreeLevelEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getGoodsList(Map<String, Object> map, String areaCode) {
        if ("".equals(areaCode)) {
            map.put("areaCode", mRepertory.getAreaCode());
        } else {
            map.put("areaCode", areaCode);
        }
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getGoodsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<GoodsListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, Response<BaseEntity<ListResponse<List<GoodsListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getGoodsList(response.body().getData().getList());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getRecommendList(Map<String, Object> map) {
//        if ("".equals(map.get("province"))) {
//            map.put("province", mRepertory.getAreaCode());
//        }
        NewAPIManager.getInstance().getGoodsAPI().getRecommendList(map).enqueue(new Callback<BaseEntity<ListResponse<List<RecommendListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<RecommendListEntity>>>> call, Response<BaseEntity<ListResponse<List<RecommendListEntity>>>> response) {
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getRecommendList(response.body().getData().getList());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<RecommendListEntity>>>> call, Throwable t) {
            }
        });
    }

    /***
     * 商品首页--自营商品
     * @param map
     */
    public void getSelfSupportMaterialsList(Map<String, Object> map) {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getSelfSupportList(map).enqueue(new Callback<BaseEntity<ListResponse<List<RecommendListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<RecommendListEntity>>>> call, Response<BaseEntity<ListResponse<List<RecommendListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getRecommendList(response.body().getData().getList());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<RecommendListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 商品首页--促销商品
     *
     * @param map
     */
    public void getPromotionsList(Map<String, Object> map) {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getPromotionsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<RecommendListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<RecommendListEntity>>>> call, Response<BaseEntity<ListResponse<List<RecommendListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getRecommendList(response.body().getData().getList());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<RecommendListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getPriceSort() {
        List<DictEntity3> list = new ArrayList<>();
        list.add(new DictEntity3("1", "从低到高"));
        list.add(new DictEntity3("2", "从高到低"));
        mFragment.showPriceSort(list);
    }

    public void getAreaChoose() {
//        DialogHelper.showProgressDialog(mFragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getAreaChoose(mRepertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<AreaChooseEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AreaChooseEntity>>> call, Response<BaseEntity<List<AreaChooseEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    mFragment.getAreaChoose(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AreaChooseEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getMenuModel(String areacode) {
//        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        if (areacode.equals("")) {
            areacode = mRepertory.getAreaCode();
        }

        map.put("industryId", "");
        map.put("areaCode", areacode);
        APIManager.getInstance().getMaterialAPI().getItemCatList(map).enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showMenuView(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    public void getConcreteList(Map<String, Object> map, String areaCode) {
        if ("".equals(areaCode)) {
            map.put("areaCode", mRepertory.getAreaCode());
        } else {
            map.put("areaCode", areaCode);
        }
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getConcreteList(map).enqueue(new Callback<BaseEntity<ListResponse<List<ConcreteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ConcreteEntity>>>> call, Response<BaseEntity<ListResponse<List<ConcreteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getConcreteList(response.body().getData().getList());
                } else {
                    MToast.showToast(mFragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ConcreteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    public String getAreaCode() {
        return mRepertory.getAreaCode();
    }
}
